const utils = {
    /**
     * @param {Function} fn 防抖函数
     * @param {Number} delay 延迟时间
     */
    debounce: function (fn, delay) {
        var timer;
        return function () {
            var context = this;
            var args = arguments;
            clearTimeout(timer);
            timer = setTimeout(function () {
                fn.apply(context, args);
            }, delay);
        };
    },
    /**
     * @param {Date} time 需要转换的时间
     * @param {String} fmt 需要转换的格式 如 yyyy-MM-dd、yyyy-MM-dd HH:mm:ss
     */
    formatTime: function (time, fmt) {
        if (!time) return '';
        else {
            const date = new Date(time);
            const o = {
                'M+': date.getMonth() + 1,
                'd+': date.getDate(),
                'H+': date.getHours(),
                'm+': date.getMinutes(),
                's+': date.getSeconds(),
                'q+': Math.floor((date.getMonth() + 3) / 3),
                S: date.getMilliseconds(),
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    (date.getFullYear() + '').substr(4 - RegExp.$1.length)
                );
            for (const k in o) {
                if (new RegExp('(' + k + ')').test(fmt)) {
                    fmt = fmt.replace(
                        RegExp.$1,
                        RegExp.$1.length === 1
                            ? o[k]
                            : ('00' + o[k]).substr(('' + o[k]).length)
                    );
                }
            }
            return fmt;
        }
    },
    /**
     * 路由跳转
     * @param {Object} that Vue实例
     * @param {String} routeName 路由名称
     * @param {String} type 跳转方式
     * @param {Object} params 携带参数
     */
    goRouter: function (that, routeName = 'index', type = 'replace', params) {
        that.$router[type]({
            name: routeName,
            params: params
        });
    },
    /**
     * @param {Number} minNum 随机数范围下限
     * @param {Number} maxNum 随机数范围上限
     */
    getRandomNum: function (minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
            case 2:
                return Math.floor(Math.random() * (maxNum - minNum + 1) + minNum);
            // return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
            default:
                return 0;
        }
    },
    /**
     * @param {Number} number 随机数范围下限
     */
    calcUnit: function (number) {
        if (number > 10000) {
            if (number < 100000000) {
                return `${number / 10000}万`
            } else {
                return `${number / 100000000}亿`
            }
        }
        return number;
    },
    /**
     * 获取页面宽高
     */
    getViewportSize: function () {
        return {
            // width: window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth,
            // height: window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight,
            width: window.screen.width,
            height: window.screen.height,
            container: [window, document],
        };
    },
    /**
     * 图表配置、数据
     * @param {Array} category 数据维度
     * @param {Array} legends 数据类型
     * @param {Array} dataRange 随机数范围  [minNum,maxNum]
     */
    initChartData: function (category = [], legends = [], dataRange = [0, 100]) {
        let cdata = {};
        let seriesData = [];
        if (legends.length > 0 && category.length > 0) {
            legends.forEach((currentLeg, indexL) => {
                seriesData[indexL] = [];
                category.forEach((currentCat, indexC) => {
                    seriesData[indexL][indexC] = utils.getRandomNum(dataRange[0], dataRange[1]);
                })
            })
        } else {
            if (category.length > 0) {
                category.forEach((currentCat, indexC) => {
                    seriesData[indexC] = utils.getRandomNum(dataRange[0], dataRange[1]);
                })
            }
            if (legends.length > 0) {
                legends.forEach((currentLeg, indexL) => {
                    seriesData[indexL] = utils.getRandomNum(dataRange[0], dataRange[1]);
                })
            }
        }
        cdata.category = category;
        cdata.legends = legends;
        cdata.seriesData = seriesData;
        return cdata;
    },
    /**
     * cancelLoading
     * @param {Object} that Vue实例
     * @param {Number} duration setTimeout延时时长
     */
    cancelLoading: function (that, duration = 500) {
        that.$nextTick(() => {
            setTimeout(() => {
                that.loading = false;
            }, duration)
        })
    },
    /**
     * enableLoading
     * @param {Object} that Vue实例
     * @param {Number} duration setTimeout延时时长
     */
    enableLoading: function (that, duration) {
        that.$nextTick(() => {
            // setTimeout(() => {
            //     that.loading = true;
            // }, duration)
            that.loading = true;
        })
    },
    /**
     * 图表组件模块获取/渲染数据
     * @param {Object} that Vue实例
     * @param {Number} duration 定时器频率
     * @param {Boolean} enableInterval 是否启用定时器，循环更新数据
     * @param {Boolean} ifFirst 是否首次
     */
    drawTimingFn: function (that, duration, enableInterval, ifFirst) {
        if (ifFirst) {
            that.setData();
            that.first = false;
        }
        if (that.drawTiming) {
            clearInterval(that.drawTiming);
            that.drawTiming = null;
        }
        if (enableInterval) {
            that.drawTiming = setInterval(() => {
                that.setData();
            }, duration * 1000);
            if (!that.$parent.openCountDown) {
                that.$nextTick(() => {
                    that.$parent.toggleCountDown();
                })
            }
        }
    },
    /**
     * 计算定时器duration
     * @param {Number} duration 时长
     * @param {String} type 单位
     */
    calcIntervalDuration: function (duration = 5000, type = 'ms') {
        switch (type) {
            case 'ms':
                return duration / 1000;
            case 's':
                return duration;
            case 'm':
                return duration * 60;
            case 'h':
                return duration * 60 * 60;
            case 'd':
                return duration * 60 * 60 * 24;
            case 'w':
                return duration * 60 * 60 * 24 * 7;
            case 'M':
                return duration * 60 * 60 * 24 * utils.getCurrentDays().monthLastDay;
            case 'y':
                return duration * 60 * 60 * 24 * utils.getCurrentDays().yearLastDay;
            default:
                return duration;
        }
    },
    /**
     * 获取当前月/年天数
     */
    getCurrentDays() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let monthLastDay = new Date(year, month, 0).getDate()//获得是标准时间,需要getDate()获得天数
        let yearLastDay = 0, d;
        for (let i = 1; i < 13; i++) {
            d = new Date(year, i, 0);//获取某一个月的天数
            yearLastDay += d.getDate();
        }
        return {
            monthLastDay: monthLastDay,
            yearLastDay: yearLastDay,
        }
    },
};
export default utils